package eerbaar.elements.extension;

import java.util.Date;
import java.util.Random;

import eerbaar.elements.IElement;
import eerbaar.extension.IExtensible;
import eerbaar.internal.elements.Element;
import eerbaar.position.IPosition;
import eerbaar.position.PositionFactory;

public class ElementFactory {

    public static IElement create(String name, int x, int y) {
        return newElement(name, PositionFactory.createPosition(x, y));
    }

    public static <T extends IExtensible> IElement create(String name, int x,
            int y, Class<T> type, T extension) {

        Element result = newElement(name, PositionFactory.createPosition(x, y));
        result.installExtension(type, extension);

        return result;
    }

    public static IElement create(String name, IPosition position) {
        return newElement(name, position);
    }

    public static IElement create(String name, IPosition position, int points) {

        Element result = newElement(name, position);
        setPoints(points, result);
        return result;
    }

    public static IElement createWithRandomPosition(String name, IPosition max) {

        Date date = new Date();
        long time = date.getTime();
        Random random = new Random(time);
        random = new Random();

        int x = random.nextInt(max.getX());
        int y = random.nextInt(max.getX());

        return newElement(name, PositionFactory.createPosition(x, y));
    }

    private static Element newElement(String name, IPosition position) {

        Element result = new Element(name, position);
        ExtentionInstaller.install(result);

        return result;
    }

    private static void setPoints(int points, Element result) {
        IPoints impl = result.getExtension(IPoints.class);
        if (impl != null) {
            impl.setPoints(points);
        }
    }

    private ElementFactory() {
        //
    }

}
